package com.cognizant.ams.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cognizant.ams.common.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.cognizant.ams.bean.Ldmenu;
import com.cognizant.ams.common.ResUI;
import com.cognizant.ams.service.MenuService;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MenuController {
	@Autowired
	private MenuService menuService;


	private  String message="";

	/*@GetMapping("/getAllMenu")
	public List<Ldmenu> getMenu() {
		List<Ldmenu> list =menuService.getAllMenu();
		//String json=JSON.toJSONString(list);
		return list;
	}*/
	
	@GetMapping("/getAllMenu1")
	@ResponseBody
	public Map<String, Object> getMenu1(int page,int limit) {
		System.out.println("当前是第"+page+"页，页面条为"+limit);
		List<Ldmenu> list =menuService.getAllMenu();
		Map<String, Object> map =new HashMap<String, Object>();
		map=ResUI.getDataFenYe(list, page, limit);
		return map;
	}

	@GetMapping("/menus")
	public ModelAndView getAllMenus () {
	ModelAndView menuModelAndView=new ModelAndView();
	List<Ldmenu> menus=menuService.getAllMenu();
	menuModelAndView.addObject("menus",menus);
	menuModelAndView.setViewName("menu/menu");
	return menuModelAndView;
	}

    @GetMapping("getmenu")
	public ModelAndView getmenu(String nodecode,String nodename,String nodesign,String cur){
	    if (null==cur){
	        cur="1";
        }
		ModelAndView modelAndView=new ModelAndView();
		Ldmenu tLdmenu=new Ldmenu();
		tLdmenu.setNodecode(nodecode);
		tLdmenu.setNodename(nodename);
		tLdmenu.setNodesign(nodesign);
		List<Ldmenu> menuList=menuService.getMenu(tLdmenu);



        int pageCount=0;
        int pageSize=10;
        if (menuList!=null&&menuList.size()>0) {
            pageCount=menuList.size()/pageSize+1;
            menuList= PageHelper.getDataByFenye(menuList, cur,pageSize);
        }
        else {
            cur="0";
        }
		//设置返回视图
		modelAndView.setViewName("menu/menu");
		//设置返回参数
        //分页数据
		modelAndView.addObject("menuList",menuList);
        modelAndView.addObject("pageCount", pageCount);
        modelAndView.addObject("cur", Integer.parseInt(cur));
        //参数回传
        modelAndView.addObject("nodecode", nodecode);
        modelAndView.addObject("nodename", nodename);
        modelAndView.addObject("nodesign", nodesign);
        return modelAndView;
	}

	@GetMapping("/addmenu")
	public ModelAndView addMenu(String nodecode,String nodename,String parentnodecode,String nodediscription,
                                String nodesign,String runscript,String nodeorder){
	    ModelAndView modelAndView=new ModelAndView();
	    modelAndView.setViewName("menu/menu");
	    Ldmenu newMenu=new Ldmenu();
	    newMenu.setNodecode(nodecode);
	    newMenu.setNodename(nodename);
	    newMenu.setParentnodecode(parentnodecode);
	    newMenu.setNodedescription(nodediscription);
	    newMenu.setNodesign(nodesign);
	    newMenu.setRunscript(runscript);
	    newMenu.setNodeorder(Integer.parseInt(nodeorder));
        Map messageMap=new HashMap();
	    int result=0;

	    try {
	        result=menuService.insertMenu(newMenu);
	        if (result>0){
	          message="菜单节点插入成功！";

            }else {
	            message="菜单新增失败";
            }
        }catch (Exception e){
	        e.printStackTrace();
	        message="菜单新增失败";
        }
        messageMap.put(result,message);
        modelAndView.addObject("pageCount", 0);
        modelAndView.addObject("cur", 0);
        modelAndView.addObject("message",message);
        modelAndView.addObject("code",result);
	    return  modelAndView;
    }

    @PostMapping("/deletemenu")
    public ModelAndView deleteMenu(String nodecode){
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("menu/menu");

        Map messageMap=new HashMap();
        int result=0;

        try {
            result=menuService.deteteMenu(nodecode);
            if (result>0){
                message="菜单节点删除成功！";

            }else {
                message="菜单删除失败";
            }
        }catch (Exception e){
            e.printStackTrace();
            message="菜单删除失败";
        }
        messageMap.put(result,message);
        modelAndView.addObject("pageCount", 0);
        modelAndView.addObject("cur", 0);
        modelAndView.addObject("message",message);
        modelAndView.addObject("code",result);
        return  modelAndView;
    }

}
